package com.lonely.fx;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Pagination;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @date: 2022-07-18 22:35
 * @author: ztkj-hzb
 * @description: 分页控件
 **/
public class PaginationSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();

        //创建分页插件
        Pagination pagination = new Pagination();
        pagination.setPrefWidth(400);
        pagination.setPrefHeight(400);

        //设置总页数，默认为Integer.max_value
        pagination.setPageCount(15);

        //设置当前选择页索引，索引从0开始
        pagination.setCurrentPageIndex(2);

        //设置指示器的最大数量，默认为10
        pagination.setMaxPageIndicatorCount(5);

        //设置样式
        pagination.setStyle("-fx-background-color: pink");

        //设置分页按钮样式
        //pagination.getStyleClass().add(Pagination.STYLE_CLASS_BULLET);

        pagination.setPageFactory(new Callback<Integer, Node>() {

            /**
             * param:当前选择的索引
             * @param param
             * @return
             */
            @Override
            public Node call(Integer param) {

                HBox box = new HBox();

                if(param == 0){
                    box.setStyle("-fx-background-color: red");
                }else if(param == 1){
                    box.setStyle("-fx-background-color: blue");
                }else {
                    box.setStyle("-fx-background-color: green");
                }
                return box;
            }
        });


        //选择分页事件
        pagination.currentPageIndexProperty().addListener((observable, oldValue, newValue) -> System.out.println(String.format("当前选择第%s页", (newValue.intValue() + 1))));

        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(pagination);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();
    }
}
